<?php
// 微信端 首页
namespace H5\Controller;
use Think\Controller;
class IndexController extends CommonController {
    public function index(){
    	/*$arr = M('user')->where(array('id'=>1))->find();
		session('userid',$arr['id']);
		session('realname',$arr['realname']);
		session('level',$arr['level']);*/
		// p($_SESSION);die;
    	$this->assign('menu','index'); // 首页
        $this->display();
    }

    // 首页 提醒事件的查询
    public function remind(){
        $userid = session('userid');
        $today = date('Y-m-d'); // 当前年月日
        $today_m = date('m');   // 当前月
        $today_d = date('d');   // 当前日
        $data = array();  // 传给前台的数组

        // 查询外勤拜访 提醒
        $map1['uid'] = $userid;
        $map1['appoint_time'] = array( 'BETWEEN' , array(strtotime($today), strtotime($today)+86399) );
        $outList = M('out')->where($map1)->field('id,cid,appoint_time')->order('appoint_time desc')->select();
        if($outList){
            foreach($outList as $k => $val){
                $outList[$k]['client_name'] = M('client')->where(array('id'=>$val['cid']))->getField('client_name');
                $outList[$k]['appoint_time'] = date('Y-m-d H:i',$outList[$k]['appoint_time']);
            }
            $data['out']['state'] = 200;
            $data['out']['content'] = $outList;
        }else{
            $data['out']['state'] = 500;
        }
        
        // 查询客户店庆
        $map2['uid'] = $userid;
        $map2['shop_birthday'] = array( 'BETWEEN' , array(strtotime($today), strtotime($today)+86399) );
        $clientList = M('client')->where($map2)->field('id,client_name,shop_birthday')->order('id desc')->select();
        if($clientList){
            foreach($clientList as $k => $val){
                if($today_m == date('m', $val['shop_birthday']) && $today_d == date('d', $val['shop_birthday'])){
                    $clientList[$k]['shop_birthday'] = date('Y-m-d',$clientList[$k]['shop_birthday']);
                    $data['client']['content'][] = $clientList[$k];
                }
            }
            if($data['client']['content']){
                $data['client']['state'] = 200;
            }else{
                $data['client']['state'] = 500;
            }
        }else{
            $data['client']['state'] = 500;
        }

         // 查询负责人生日
        $map3['uid'] = $userid;
        $map3['birthday'] = array( 'BETWEEN' , array(strtotime($today), strtotime($today)+86399) );
        $contactList = M('contact')->where($map3)->field('id,cid,realname,birthday')->order('id desc')->select();
        if($contactList){
            foreach($contactList as $k => $val){
                if($today_m == date('m', $val['birthday']) && $today_d == date('d', $val['birthday'])){
                    $contactList[$k]['client_name'] = M('client')->where(array('id'=>$val['cid']))->getField('client_name');
                    $contactList[$k]['birthday'] = date('Y-m-d',$contactList[$k]['birthday']);
                    $data['contact']['content'][] = $contactList[$k];
                }
            }
            if($data['contact']['content']){
                $data['contact']['state'] = 200;
            }else{
                $data['contact']['state'] = 500;
            }
        }else{
            $data['contact']['state'] = 500;
        }

        //p($data);

        $this->ajaxReturn($data);

    }
}